<!DOCTYPE html>
<html>
<head>
<link rel="Stylesheet" type="text/css" href="style.css">
<title>fish shell</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<div id="Contents" class="toc"><h1 id="Contents">Contents</a></h1></div>
<ul>
<li>
<a href="fish shell.html#The compatability with bash">The compatability with bash</a>

<li>
<a href="fish shell.html#Parameter expansion">Parameter expansion</a>

<li>
<a href="fish shell.html#Command expansion">Command expansion</a>

<li>
<a href="fish shell.html#Bracket expansion">Bracket expansion</a>

<li>
<a href="fish shell.html#Variable expansion">Variable expansion</a>

<li>
<a href="fish shell.html#List">List</a>

<li>
<a href="fish shell.html#Cartesian product">Cartesian product</a>

<li>
<a href="fish shell.html#Index range">Index range</a>

<li>
<a href="fish shell.html#Variable scope">Variable scope</a>

<li>
<a href="fish shell.html#Control structure">Control structure</a>

<ul>
<li>
<a href="fish shell.html#Control structure-Condition">Condition</a>

<li>
<a href="fish shell.html#Control structure-Loop">Loop</a>

<li>
<a href="fish shell.html#Control structure-Logical operator">Logical operator</a>

</ul>
<li>
<a href="fish shell.html#Auxiliary command">Auxiliary command</a>

<li>
<a href="fish shell.html#Vim mode">Vim mode</a>

<li>
<a href="fish shell.html#Initialization files">Initialization files</a>

</ul>

<div id="The compatability with bash"><h1 id="The compatability with bash" class="header"><a href="#The compatability with bash">The compatability with bash</a></h1></div>
<p>
<span id="The compatability with bash-IO redirection"></span><strong id="IO redirection">IO redirection</strong>
</p>

<p>
<code>&amp;&gt;</code>:redirect both stdout and stderr.
</p>

<p>
<code>&gt;?</code> and <code>2&gt;?</code>: like <code>&gt;</code> and <code>2&gt;</code>, except that they don't overwrite existed file.
</p>

<p>
<span id="The compatability with bash-Pipe"></span><strong id="Pipe">Pipe</strong>
Fish allow user customize the source of pipe(stdout, stderr, both) using <code>2&gt;|</code>(stderr) or <code>&amp;|</code>(both). Bash can pipe both stdout and stderr with <code>|&amp;</code>.
</p>

<p>
<span id="The compatability with bash-Status"></span><strong id="Status">Status</strong>
in Bash, <code>\(?</code> represents the return value of command. But in fish,<code> </code><code>\)status</code> represents the return value of command.
</p>

<div id="Parameter expansion"><h1 id="Parameter expansion" class="header"><a href="#Parameter expansion">Parameter expansion</a></h1></div>
<p>
When an argument for a program is given on the commandline, it undergoes the process of parameter expansion before it is sent on to the command. If any wildcard fails to expand, the command is note executed. There are exactly 4 exceptions, namely <code>set</code>, overriding variables in <code>overrides</code>, <code>count</code> and <code>for</code>.
</p>

<div id="Command expansion"><h1 id="Command expansion" class="header"><a href="#Command expansion">Command expansion</a></h1></div>
<p>
??
</p>

<div id="Bracket expansion"><h1 id="Bracket expansion" class="header"><a href="#Bracket expansion">Bracket expansion</a></h1></div>

<p>
The syntax <code>{...}</code> is not glob pattern. Actually, it is bracket expansion. A comma separated list of characters enclosed in curly braces will be expanded so each element of the list becomes a new parameter. Bracket expansion must contain at least one comma, If two braces do not contain a "," or a variable expansion, they will not be expanded in this manner.
</p>

<pre sh>
    &gt; echo foo-{}
    foo-{}
    &gt; git reset --hard HEAD@{2}
    # passes "HEAD@{2}" to git
    &gt; echo {{a,b}}
    {a} {b} # because the inner brace pair is expanded, but the outer isn't.
</pre>

<p>
If there is nothing between a brace and a comma or two commas, it's interpreted as an empty element:
</p>
<pre sh>
    &gt; echo {,,/usr}/bin
    /bin /bin /usr/bin
</pre>

<div id="Variable expansion"><h1 id="Variable expansion" class="header"><a href="#Variable expansion">Variable expansion</a></h1></div>


<pre sh>
    echo The plural of $WORD is "$WORD"s
    # Prints "The plural of cat is cats" when $WORD is set to cat.
    echo The plural of $WORD is {$WORD}s
    # ditto
</pre>

<p>
If, in the example above, \(WORD is undefined or an empty list, the "s" is not printed. However, it is printed if \)WORD is the empty string (like after set WORD "").
</p>

<p>
Deu to the restriction of barcket expansion, If we want to combine vairable expansion and other string, should use this syntax:
</p>
<pre sh>
    echo {$var}-test
</pre>

<p>
Fish has the ability of multiple variable expansion.
</p>
<pre sh>
    set foo a b c
    set a 10; set b 20; set c 30
    for i in (seq (count $$foo))
        echo $$foo[$i]
    end

    # Output is:
    # 10
    # 20
    # 30
</pre>

<div id="List"><h1 id="List" class="header"><a href="#List">List</a></h1></div>
<p>
The index of list starts from 1 not 0.
</p>

<p>
List cannot contain list recursively. If want to get multi-dimensional list, please use <span id="List-multiple_variable_expansion"></span><strong id="multiple_variable_expansion">multiple_variable_expansion</strong>.
</p>

<p>
All lists whose name ends in 'PATH' would be splitted by colons.  
</p>

<div id="Cartesian product"><h1 id="Cartesian product" class="header"><a href="#Cartesian product">Cartesian product</a></h1></div>
<p>
Happend cartesian product only when list is adjcent to list or string.
</p>
<ul>
<li class="done0">
涉及 command expansion 的部分

</ul>

<div id="Index range"><h1 id="Index range" class="header"><a href="#Index range">Index range</a></h1></div>
<p>
Variables can be used as indices for expansion of variables, like so:
</p>

<pre sh>
set index 2
set letters a b c d
echo $letters[$index] # returns 'b'
</pre>

<p>
However using variables as indices for command substitution is currently not supported, so:
</p>
<pre sh>
echo (seq 5)[$index] # This won't work

set sequence (seq 5) # It needs to be written on two lines like this.
echo $sequence[$index] # returns '2'

</pre>



<div id="Variable scope"><h1 id="Variable scope" class="header"><a href="#Variable scope">Variable scope</a></h1></div>

<ul>
<li>
If a variable is explicitly set to a scope (universal, global or local), that setting will be honored. If a variable of the same name exists in a different scope, that variable will not be changed.

<li>
If a variable is not explicitly set to a scope, but has been previously defined, the variable scope is not changed.

<li>
If a variable is not explicitly set to a scope and has not been defined, the variable will be local to the currently executing function. Note that this is different from using the -l or --local flag. If one of those flags is used, the variable will be local to the most inner currently executing block, while without these the variable will be local to the function. If no function is executing, the variable will be global.

</ul>

<div id="Control structure"><h1 id="Control structure" class="header"><a href="#Control structure">Control structure</a></h1></div>

<div id="Control structure-Condition"><h2 id="Condition" class="header"><a href="#Control structure-Condition">Condition</a></h2></div>

<pre sh>
    if cond1
        # do something
    else if cond2
        # do something
    else
        # do something
    end
</pre>

<pre sh>
    switch var
        case constexpr1
        # do something
        case constexpr2
        # do something
        case '*'        # wildcard
        # do something
    end
</pre>

<p>
If case block has multiple lines, wrap it using <code>begin</code> and <code>end</code>. Switch statement is not fall-through.
</p>
<div id="Control structure-Loop"><h2 id="Loop" class="header"><a href="#Control structure-Loop">Loop</a></h2></div>
<pre sh>
    while cond
        # do something
    end
</pre>

<pre sh>
    for var in [list]
        # do something
    end
</pre>

<p>
Usually use <code>count</code> and <code>seq</code> create a list.
</p>
<pre sh>
    set -l var a b c d
    # (count $var) return 4.
    # if don't wrap `seq` and `count $var`, fish treat them as two string.
    for i in (seq (count $var))
        echo $i
    end
</pre>

<div id="Control structure-Logical operator"><h2 id="Logical operator" class="header"><a href="#Control structure-Logical operator">Logical operator</a></h2></div>
<p>
<code>and</code>: If previous command executes successfully, will execute the following command.
</p>

<p>
or:  If previous command fails to execute, will execute the following command.
</p>

<p>
The return status <code>status</code> is the exit code of last executed command.
</p>

<pre sh>
    if ls; and cat ls.txt
        echo yes
    else
        echo no      
    end
</pre>

<div id="Auxiliary command"><h1 id="Auxiliary command" class="header"><a href="#Auxiliary command">Auxiliary command</a></h1></div>
<div id="Auxiliary command-`set` command"><h2 id="`set` command" class="header"><a href="#Auxiliary command-`set` command"><code>set</code> command</a></h2></div>

<div id="Auxiliary command-`count` command"><h2 id="`count` command" class="header"><a href="#Auxiliary command-`count` command"><code>count</code> command</a></h2></div>

<div id="Auxiliary command-`seq` command"><h2 id="`seq` command" class="header"><a href="#Auxiliary command-`seq` command"><code>seq</code> command</a></h2></div>

<div id="Auxiliary command-`test` command"><h2 id="`test` command" class="header"><a href="#Auxiliary command-`test` command"><code>test</code> command</a></h2></div>

<div id="Auxiliary command-`read` command"><h2 id="`read` command" class="header"><a href="#Auxiliary command-`read` command"><code>read</code> command</a></h2></div>


<div id="Vim mode"><h1 id="Vim mode" class="header"><a href="#Vim mode">Vim mode</a></h1></div>

<div id="Initialization files"><h1 id="Initialization files" class="header"><a href="#Initialization files">Initialization files</a></h1></div>

</body>
</html>
